package com.lenovo.chartdemo;

import android.content.Context;
import android.graphics.Color;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;

/*********************************************************************************
 Created by Android Studio.
 *Author:          Jack Fu
 *Version:         1.0
 *Date;            17-5-29 上午11:12
 *Description:     
 **********************************************************************************/
public class StackedBarChartUtil {
    public static BarChart getChart(Context context){
        BarChart mChart = new BarChart(context);
        mChart.setRotation(90);
        mChart.setScaleY(0.5f);
        mChart.setScaleX(0.5f);
        mChart.setDescription("");

        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        mChart.setMaxVisibleValueCount(60);

        // scaling can now only be done on x- and y-axis separately
        mChart.setPinchZoom(false);

        mChart.setDrawGridBackground(false);
        mChart.setDrawBarShadow(false);

        mChart.setDrawValueAboveBar(true);

        // change the position of the y-labels
        YAxis yLabels = mChart.getAxisLeft();
        yLabels.setEnabled(false);
        mChart.getAxisRight().setEnabled(false);

        XAxis xLabels = mChart.getXAxis();
        xLabels.setEnabled(false);
        xLabels.setPosition(XAxis.XAxisPosition.TOP);

        // mChart.setDrawXLabels(false);
        // mChart.setDrawYLabels(false);

        // setting data

        Legend l = mChart.getLegend();
        l.setEnabled(false);

        setData(1256,mChart);

        // mChart.setDrawLegend(false);
        return mChart;
    }

    private static void setData(final int value, BarChart mChart) {
        int colors[] = {
                Color.RED,Color.RED,Color.GREEN, Color.RED,
                Color.BLUE,Color.BLUE,Color.WHITE
        };
        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < 2; i++) {
            xVals.add(i+"");
        }

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        int min = 1100;
        final int max = 3190;
        int start = 0;
        final float end = 4000;
        final float bar13 = max  - min > 0 ? max  - min : 0;
        final float bar14 = end - max > 0 ? end - max  : 0;
        final float bar23 = end - value > 0 ? end - value :0;

        yVals1.add(new BarEntry(new float[] { start,min, bar13,bar14}, 0));
        yVals1.add(new BarEntry(new float[] { 0,value, bar23}, 1));

        BarDataSet set1 = new BarDataSet(yVals1, "");
        set1.setColors(colors);
        set1.setStackLabels(new String[] { "Births", "Divorces", "Marriages" });

        ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
        dataSets.add(set1);

        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(20);
        data.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float v, Entry entry, int i, ViewPortHandler viewPortHandler) {
                if(v == bar13){
                    return max+"";
                }else if(v == bar14){
                    return end+"";
                }else if(v == bar23){
                    return end+"";
                }
                return v+"";
            }
        });

        mChart.setData(data);
        mChart.invalidate();
    }


}
